package com.bjsj.pro.ui;


import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.bjsj.pro.R;
import com.bjsj.pro.base.AAAPresentr;
import com.bjsj.pro.base.MVPBaseActivity;
import com.bjsj.pro.base.NullView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Nevermore on 2018/1/20.
 * <p>
 * demo界面,方便复制
 */

public class WebMainActivity extends MVPBaseActivity<NullView, AAAPresentr> implements
        NullView {

    @BindView(R.id.webView)
    WebView webView;

    @Override
    protected AAAPresentr createPresenter() {
        return new AAAPresentr(this);
    }


    @Override
    protected int setLayoutResId() {
        return R.layout.web_activity_main;
    }

    @Override
    protected void initData() {
        //setFullScreen();

        showTitleBarLayout(true,"博江小帮手",null,R.drawable.round_login);
        WebSettings webSettings = webView.getSettings();
        // WebSettings webSettings = wvAgreement.getSettings();
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);
        //允许android调用javascript
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);


        webSettings.setBuiltInZoomControls(true);
        webSettings.setAllowFileAccess(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setAllowUniversalAccessFromFileURLs(true);
        }
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int mDensity = metrics.densityDpi;
        if (mDensity == 240) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == 160) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        } else if (mDensity == 120) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        } else if (mDensity == DisplayMetrics.DENSITY_XHIGH) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == DisplayMetrics.DENSITY_TV) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        }

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.requestFocus();
        //点击超链接的时候重新在原来的进程上加载URL
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                Log.e("打印Scheme", uri.getScheme() + "==" + url);
                if (!"http".equals(uri.getScheme()) || !"https".equals(uri.getScheme())) {
                    return false;
                } else {
                    view.loadUrl(url);
                    return false;
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                Toast toast = Toast.makeText(WebMainActivity.this, message, Toast.LENGTH_SHORT);
                toast.show();
                result.confirm();
                return true;
            }
        });
        webView.loadUrl("https://www1.hnbjsj.com");
        //mTitleBarLayout.setLefeShow(true, R.drawable.ic_back, null);

    }


    private static long exitTime = 0;

    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast toast = Toast.makeText(WebMainActivity.this, "再按一次退出！", Toast.LENGTH_SHORT);
            toast.show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
            if (webView.canGoBack())
            {
                webView.goBack(); //goBack()表示返回WebView的上一页面
                return true;
            }else
            {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    Toast toast = Toast.makeText(WebMainActivity.this, "再按一次退出！", Toast.LENGTH_SHORT);
                    toast.show();
                    exitTime = System.currentTimeMillis();
                } else {
                    finish();
                    System.exit(0);
                    return true;
                }

            }

        }
        return false;
    }

    @Override
    protected void initView() {

    }


}
